<?php
class Browser {
    // Define the public variables
    public $Device = "Unknown";     // The actual device name
    public $Firmware = Array(       // Firmware
        "version" => "",            // Full Firmware String
        "device_code" => ""         // Short Code for this Device
    );
    public $Resolution = Array(     // Resolution
        "display" => "SD",          // Display Resolution (HD or SD)
        "tv" => Array(              // Exact TV Resolution
            "width" => 0,
            "height" => 0
        ),
        "browser" => Array(         // Exact Browser Resolution
            "width" => 0,
            "height" => 0
        )
    );
    public $MAC = "";               // MAC Address of the device
    public $IsNMT = false;          // Is this device an NMT?
    public $UserAgent = "";         // The full (original) UserAgent string
    
    // Define the NMT Types
    private $NMTType = Array(
        "FIA-101" => "FIA On3",
        "PIN-101" => "Pinnacle ShowCenter 1000",
        "ADS-102" => "ADS Tech Media-Link MXL-581",
        "EGT-103" => "Elgato EyeHome",
        "NST-105" => "Neuston Virtuoso MC-500",
        "LTI-107" => "Transgear DVX-500E",
        "MMS-108" => "Momitsu V880N",
        "LTI-121" => "LiteOn LVD-2010",
        "???-200" => "ViewSonic HDMR-2000",
        "DGR-230" => "DigitalRise Xstream Player",
        "HLO-230" => "Helios Labs X5000",
        "PIN-230" => "Pinnacle ShowCenter 200/250HD",
        "QUA-230" => "Quartek WHD-500-V9",
        "SYB-230" => "RedBell DV HD100",
        "CTL-231" => "Quartek WHD-500-V9",
        "LTI-232" => "Buffalo LinkTheater (JA)",
        "IOD-234" => "I-O Data AveL Linkplayer2",
        "LTI-236" => "Transgear DVX-600",
        "LTI-240" => "Buffalo LinkTheater (US)",
        "VNE-242" => "Snazio SZ1310",
        "DLT-252" => "Transgear DVX-700",
        "TTI-252" => "Transgear DVX-700 TypeII",
        "LTI-254" => "Buffalo LinkTheater (EU)",
        "IOD-255" => "I-O Data SRDVD-100U",
        "VNE-258" => "Snazio SZ1350",
        "HNB-260" => "H&B DNX-8620",
        "HLO-288" => "Helios Labs X3000",
        "OMI-290" => "Omisys DVD PN-210V",
        "NGR-293" => "Netgear EVA700",
        "CMI-402" => "CMI SYVIO-200A",
        "RTD-402" => "Dragontech ioBox-100HD",
        "EGR-402" => "Egreat EG-M31A",
        "ELE-402" => "Elektron EHP-600",
        "HDD-402" => "HDX 900",
        "QPG-402" => "ISTAR Mini HD 1.1",
        "KFA-402" => "Kaifa EP3000",
        "POP-402" => "Popcorn Hour A-100",
        "AMS-402" => "Suno SN-HD101",
        "EGR-403" => "Egreat EG-M31B/M34A",
        "HDD-403" => "HDX 1000",
        "DST-403" => "Icy Box IB-MP309",
        "QPG-403" => "ISTAR Mini HD 1.3",
        "KFA-403" => "Kaifa EP6000B",
        "POP-403" => "Popcorn Hour A-110",
        "ELE-404" => "Elektron EHP-606",
        "EGR-405" => "Egreat EG-M32B",
        "POP-405" => "Popcorn Hour B-110",
        "KBR-406" => "Kaiboer K100/K200",
        "POP-408" => "Popcorn Hour C-200",
        "POP-411" => "Popcorn Hour A-200",
    );

    public function Browser() {
        // Get the current UserAgent string
        $_UserAgent = $_SERVER['HTTP_USER_AGENT'];
    
        $this->UserAgent = $_UserAgent;
        if (strlen($_UserAgent) >= 6) {
            // Check if it starts with "Syabas"
            $this->IsNMT = (strtolower(substr($_UserAgent,0,6)) === "syabas");
        } else {
            // User Agent too short: Unknown Browser
            $this->IsNMT = false;
        }
        
        if ($this->IsNMT) {
            preg_match("/(syabas\/.*) \((.*)/i", $_UserAgent, $Matches);
            if (count($Matches) >= 3) {
                // 0 = Full Match, 1 = Browser + Firmware Version, 2 = Resolution + MAC Address
                // Explode the first part to get the Firmware version and Devide Type
                $_TempInfo = explode("/", $Matches[1]);
                if (count($_TempInfo) >= 4) {
                    // 0 = Syabas, 1 = Full Firmware Version, 2 = Browser, 3 = ??Version??
                    $this->Firmware['version'] = $_TempInfo[1];
                    
                    // Get the Device according to the firmware
                    while (list($Code,$DeviceName) = each($this->NMTType)) {
                        if (preg_match("/.*\-(" . str_replace("?","\\?",$Code) . ").*/i",$_TempInfo[1],$_TempArray)) {
                            $this->Device = $DeviceName;
                            $this->Firmware['device_code'] = $Code;
                            break;
                        }
                    }
                }
                
                // Explode the second part to get the Browser and TV Resolutions
                $_TempInfo = explode(";", $Matches[2]);
                foreach ($_TempInfo as $Info) {
                    preg_match("| (.*)Res(\d*)x(\d*).*|i", $Info, $_TempArray);
                    if (count($_TempArray) >= 4) {
                        // get the resolutions
                        // 0 = Full Match, 1 = Resolution Type (Browser, TV), 2 = Width, 3 = Height
                        if ((trim($_TempArray[1]) != "") && (trim($_TempArray[2]) != "") && (trim($_TempArray[3]) != "")) {
                            $this->Resolution[trim(strtolower($_TempArray[1]))]['width'] = $_TempArray[2];
                            $this->Resolution[trim(strtolower($_TempArray[1]))]['height'] = $_TempArray[3];
                        }
                    } else {
                        // check if this is the MAC Address field
                        preg_match("|.*mac_addr=(([A-F0-9][A-F0-9]\.){5}[A-F0-9][A-F0-9]).*|i", $Info, $_TempArray);
                        if ((count($_TempArray) >= 2) && (trim($_TempArray[1]) != "")) {
                            $this->MAC = trim($_TempArray[1]);
                        }
                    }
                }
                
                // Check if display is HD or SD
                $this->Resolution['display'] = ($this->Resolution['tv']['height'] >= 720) ? "HD" : "SD";
            }
        } else {
            $this->Device = "PC";
            $this->Resolution['display'] = "HD";
        }
    }
    
    public function DisplayInfo() {
        if ($this->IsNMT) {
            print "Device Type: Networked Media Tank<br>";
            print "Device Name: " . $this->Device . "<br>";
            print "Device MAC Address: " . $this->MAC . "<br>";
            print "Firmware version: " . $this->Firmware['version'] . "<br>";
            print "Firmware device code: " . $this->Firmware['device_code'] . "<br>";
            print "Resolution Display: " . $this->Resolution['display'] . "<br>";
            print "Resolution TV: " . $this->Resolution['tv']['width'] . "x" . $this->Resolution['tv']['height'] . "<br>";
            print "Resolution Browser: " . $this->Resolution['browser']['width'] . "x" . $this->Resolution['browser']['height'] . "<br>";
            print "User Agent String: " . $this->UserAgent . "<br>";
        } else {
            print "Device Type: Unknown, probably PC<br>";
            print "Display Resolution: " . $this->Resolution['display'] . "<br>";
            print "User Agent String: " . $this->UserAgent . "<br>";
        }
    }
}
?>